package com.bbcare.oms.plat.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtil;
import com.bbcare.oms.plat.entity.Account;
import com.bbcare.oms.plat.entity.AccountApplication;
import com.bbcare.oms.plat.entity.Auth;
import com.bbcare.oms.plat.service.IAccountService;

/**
 * 变更历史：
 * 2023/5/4:根据5/4周报，广三第23项工作任务进行升级。
 */
@Controller
@ResponseBody
@RequestMapping({"/auth"})
public class AuthController {
  private static final Log logger = LogFactory.getLog(AuthController.class);

  @Autowired
  private IAccountService accountService;

  @RequestMapping({"/login"})
  public ResultModel<Account> login(String username, String password, HttpSession httpSession, HttpServletRequest request) throws Exception {
    logger.info("username:" + username);
    String ipaddress = ToolUtil.getIPAddress(request);
    logger.info("ipaddress:" + ipaddress);
    Account account = this.accountService.doLogin(username, password, ipaddress);
    logger.info("account:" + account);
    httpSession.setAttribute("account", account);
    return new ResultModel(account);
  }

  @RequestMapping({"/autoLogin"})
  public ResultModel<Account> autoLogin(String username, HttpSession httpSession, HttpServletRequest request) throws Exception {
    if (StringUtils.isEmpty(username)) {
      throw new Exception("用户名不能为空");
    } else {
      String ipaddress = ToolUtil.getIPAddress(request);
      Account account = this.accountService.autologin(username, ipaddress);
      httpSession.setAttribute("account", account);
      return new ResultModel(account);
    }
  }

  @RequestMapping({"/logout"})
  public ResultModel<Object> logout(HttpSession httpSession) throws Exception {
    Account account = (Account)httpSession.getAttribute("account");
    this.accountService.doLogout(account.getSessionID());
    return new ResultModel((Object)null);
  }

  /**
   * 变更历史：
   * 2023/5/4修改。
   * @param appId
   * @param httpSession
   * @param sessionId
   * @param request
   * @return
   * @throws Exception
   */
  @RequestMapping({"/app/switch"})
  public ResultModel<Account> switchApp(String appId, HttpSession httpSession, String sessionId, HttpServletRequest request) throws Exception {
    Account account = (Account)httpSession.getAttribute("account");
    if (account == null) {
      account = this.accountService.getRedis(sessionId);
      if (account == null) {
        throw new Exception("请登录后再操作");
      }
    }

    String username = account.getUsername();
    String ipaddress = ToolUtil.getIPAddress(request);
    account = this.accountService.switchApp(appId, account, ipaddress);
    final String doctorPhone1 = "13763332666";  //谭主任的登录手机号。
    final String doctorPhone2 = "13450282232";  //陈倩医生的登录手机号。
    //上述2个登录账号需要使用下载EXCEL的功能。
    if (!username.equalsIgnoreCase(doctorPhone1) && !username.equalsIgnoreCase(doctorPhone2) ) {
      AccountApplication app = (AccountApplication)account.getApps().get(0);
      List<Auth> authList = app.getNavbars();
      int authSize = authList.size();
      for(int i = 0; i != authSize; ++i) {
        if (((Auth)authList.get(i)).getId().equalsIgnoreCase("gzykdxfsdsyymu014")) {
          authList.remove(i);
          break;
        }
      }
    }

    return new ResultModel(account);
  }

  @RequestMapping({"/app/switchProject"})
  public ResultModel<Map<String, Object>> switchProject(String appId, String projectId, String sessionID, HttpSession httpSession, HttpServletRequest request) throws Exception {
    Account account = (Account)httpSession.getAttribute("account");
    if (account == null) {
      throw new Exception("请登录后再操作");
    } else {
      String ipaddress = ToolUtil.getIPAddress(request);
      Map<String, Object> map = this.accountService.switchProject(appId, projectId, account, sessionID, ipaddress);
      return new ResultModel(map);
    }
  }

  @RequestMapping({"/app/modifyPassWord"})
  public ResultModel<Map<String, Object>> modifyPassWord(String username, String password, String newPassword, HttpSession httpSession, HttpServletRequest request) throws Exception {
    Map<String, Object> map = this.accountService.modifyPassWord(username, password, newPassword);
    return new ResultModel(map);
  }
}
